/*
 * FaceCat图形通讯框架
 * 著作权编号:2015SR229355+2020SR0266727
 * 上海卷卷猫信息技术有限公司
 */

#include "FCHttpPostService.h"

namespace FaceCat{
    FCHttpPostService::FCHttpPostService(){
        m_isSyncSend = true;
        m_timeout = 0;
    }
    
    FCHttpPostService::~FCHttpPostService(){
    }
    
    FCLock FCHttpPostService::getLock(){
        return m_lock;
    }
    
    bool FCHttpPostService::isSyncSend(){
        return m_isSyncSend;
    }
    
    void FCHttpPostService::setIsSyncSend(bool value){
        m_isSyncSend = value;
    }
    
    int FCHttpPostService::getTimeout(){
        return m_timeout;
    }
    
    void FCHttpPostService::setTimeout(int value){
        m_timeout = value;
    }
    
    lstring FCHttpPostService::getUrl(){
        return m_url;
    }
    
    void FCHttpPostService::setUrl(lstring value){
        m_url = value;
    }
    
    //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
    void FCHttpPostService::onReceive(FCMessage *message){
        FCClientService::onReceive(message);
        sendToListener(message);
    }
    
    String FCHttpPostService::post(const String& url){
        lstring result = post(url, 0, 0);
        String text = FCTran::stringToString(result);
        return text;
    }
    
    lstring FCHttpPostService::post(const String& url, char *sendDatas, int sendSize){
        lstring surl = FCTran::StringTostring(url);
        NSString *nUrl = [NSString stringWithUTF8String: surl.c_str()];
        NSURL *URL = [NSURL URLWithString:nUrl];
        NSMutableURLRequest *request=[NSMutableURLRequest requestWithURL:URL];
        request.HTTPMethod = @"POST";//设置请求方法
        if(sendDatas){
            NSData *nsBody = [NSData dataWithBytes:sendDatas length:sendSize];
            request.HTTPBody = nsBody;
        }
        NSURLResponse * response = nil;
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:&response
                                                         error:&error];
        if(error){
            return "";
        }
        else{
            char *resData = (char *) [data bytes];
            return resData;
        }
    }
    
    String FCHttpPostService::post( const String& url, const String &data ){
        lstring sData = FCTran::StringTostring(data);
        lstring str = post(url, (char*)sData.c_str(), (int)sData.length() + 1);
        String wResult = FCTran::stringToString(str);
        return wResult;
    }
    
    int FCHttpPostService::send(FCMessage *message){
        return sendRequest(message);
    }
    
    int FCHttpPostService::sendRequest(FCMessage *message){
        Long upFlow = FCClientService::getUpFlow();
        FCBinary binary;
        char *buffer = 0;
        const char *body = message->m_body;
        int bodyLength = message->m_bodyLength;
        int uncBodyLength = bodyLength;
        if (message->m_compressType == COMPRESSTYPE_GZIP){
            //TODO
        }
        
        int len = sizeof(int) * 3 + bodyLength + sizeof(short) * 2 + sizeof(char) * 2;
        binary.initialize(len, false);
        binary.writeInt(len);
        binary.writeShort((short)message->m_serviceID);
        binary.writeShort((short)message->m_functionID);
        binary.writeInt(message->m_requestID);
        binary.writeChar((char)message->m_state);
        binary.writeChar((char)message->m_compressType);
        binary.writeInt(uncBodyLength);
        binary.write(body, bodyLength);
        char *str = (char*)binary.getBytes();
        if(buffer){
            delete[] buffer;
            buffer = 0;
        }
        
        NSString *nUrl = [NSString stringWithUTF8String: getUrl().c_str()];
        NSURL *URL = [NSURL URLWithString:nUrl];
        NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:URL];
        request.HTTPMethod = @"POST";//设置请求方法
        NSData *nsBody = [NSData dataWithBytes:str length:len];
        request.HTTPBody = nsBody;
        
        NSURLResponse * response = nil;
        NSError *error = nil;
        NSData *data = [NSURLConnection sendSynchronousRequest:request
                                             returningResponse:&response
                                                         error:&error];
        char *resData = (char *) [data bytes];
        int ret = [data length];
        upFlow += ret;
        FCClientService::setUpFlow(upFlow);
        
        FCClientService::callBack(message->m_socketID, 0, resData, ret);
        return ret;
    }
}
